package scope

import "github.com/spf13/cast"

func NewColumnValues(cvs ...interface{}) ColumnValues {
	var columnValues ColumnValues
	for i := 0; i < len(cvs); i += 2 {
		columnValues = append(columnValues, ColumnValue{
			Column: cvs[i].(string),
			Value:  cvs[i+1],
		})
	}
	return columnValues
}

type ColumnValues []ColumnValue

func (cvs ColumnValues) AsMap() map[string]any {
	m := make(map[string]any)
	for _, cv := range cvs {
		key := cast.ToString(cv.Column)
		m[key] = cv.Value
	}
	return m
}

func (cvs ColumnValues) Set(k string, v any) ColumnValues {
	for i, cv := range cvs {
		if cv.Column == k {
			cvs[i].Value = v
			return cvs
		}
	}
	return append(cvs, ColumnValue{Column: k, Value: v})
}

// 值对象
type ColumnValue struct {
	Column string
	Value  interface{}
}
